<?php

require_once ROOT_APPLICATION_MODULES_ADMIN_FORMS . DS . "User.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_OBJECTS . DS . "User.php";
require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS . DS . "Users.php";

require_once ROOT_APPLICATION_MODULES_ADMIN_MODELS.DS."Users.php";

class Admin_UserController extends Zend_Controller_Action {

    ///////////////////////
    // V A R I A B L E S //
    ///////////////////////
    private $modelUser;
    private $currentUser;
    
    /////////////////////
    // O V E R I D E S //
    /////////////////////
    public function init() {
        /* Initialize action controller here */
        if (Zend_Auth::getInstance()->getIdentity() == FALSE) {
            $this->_redirect('admin/auth/login');
        }
        
        $layoutPath = APPLICATION_PATH . '/templates/admin/default';
        $option = array('layout' => 'index',
            'layoutPath' => $layoutPath);

        Zend_Layout::startMvc($option);
        
        //init variable
        //MODEL
        $this->modelUser = new End_Models_Users();
        
        //OTHER 
        $this->currentUser = $this->getInforUser();
        
        //VIEW 
        $this->view->currentUser = $this->currentUser; 
        /* Initialize action controller here */
    }

    ///////////////////
    // U T I L I E S //
    ///////////////////
    private function getInforUser() {
        try {
            $currentUser = Zend_Auth::getInstance()->getIdentity();
            return $currentUser;
        } catch (Zend_Exception $ex) {
            
        }
    }

    
    ////////////////////
    // A C T I O N S  //
    //////////////////// 
    public function addAction() {
        //Create form enter info for new Page
        $form = new Application_Form_User();

        //Set label for the button
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');

        //Set client view page form  = $form
        $this->view->form = $form;
        $this->view->bodyCopy = "<p >Nhập thông tin người dùng:</p>";
        $created_date = date('Y-m-d H:i:s');
        //If can get client request 
        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', '0');

            if ($cancel) {
                $this->_helper->redirector('index');
            }
            //Get data from form.
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {   //If data is valid 
                //Get data one by one
                $role_id = (int) $form->getValue('role_id');
                $user_name = $form->getValue('user_name');
                $password = $form->getValue('password');
                $full_name = $form->getValue('full_name');
                $email = $form->getValue('email');
                //	$is_active= (int)$form->getValue('is_active');
                //	$is_online= (int)$form->getValue('is_online');
                //Create new  User object from datas
                $user = new User(null, $role_id, $user_name, $password, $full_name, $email, 0, $created_date, null, 0);

                //Use insert function from  User model
                $this->modelUser->_insert($user);

                //Move to index page
                $this->_helper->redirector('index');
            } else {
                //Return form
                $form->populate($formData);
            }
        }
    }

    public function deleteAction() {
        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $user_id = (int) $this->getRequest()->getPost('user_id');
                $Users = new End_Models_Users();
                $Users->_delete($user_id);
            }
            $this->_helper->redirector('index');
        } else {
            $user_id = $this->_getParam('user_id', 0);
            $this->view->Users = Utility_Converter::objectToArray($this->modelUser->_find($user_id));
        }
    }

    public function editAction() {
        $user_id = $this->_getParam('user_id');
        $form = new Application_Form_User();
        $form->submit->setLabel('Đồng ý');
        $form->cancel->setLabel('Hủy bỏ');
        $this->view->form = $form;

        if ($this->getRequest()->isPost()) {
            $cancel = $this->getRequest()->getPost('cancel', 0);
            if ($cancel) {
                $this->_helper->redirector('index');
            }
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
                $role_id = (int) $form->getValue('role_id');
                $user_name = $form->getValue('user_name');
                $password = $form->getValue('password');
                $full_name = $form->getValue('full_name');
                $email = $form->getValue('email');
                //			$is_active= (int)$form->getValue('is_active');
                //			$is_online= (int)$form->getValue('is_online');

                $user = new User($user_id, $role_id, $user_name, $password, $full_name, $email, 0, null, null, 0);
                $this->modelUser->_update($user);
                $this->_helper->redirector('index');
            } else {
                $form->populate($formData);
            }
        } else {
            $user_id = $this->_getParam('user_id', 0);
            if ($user_id > 0) {
                $form->populate(Utility_Converter::objectToArray($this->modelUser->_find($user_id)));
            }
        }
    }

    public function indexAction() {
        $user = $this->modelUser->fetchAll();
        $this->view->User = $user;
    }

}

